import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController } from 'ionic-angular';
import { DialogProvider } from '../../providers/dialog/dialog';
import { BleProvider } from '../../providers/ble/ble';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { EventProvider } from '../../providers/event/event'

/**
 * Generated class for the TimeSelectPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-time-select',
  templateUrl: 'time-select.html',
})
export class TimeSelectPage {
  // 设备id
  power = {
    power_unit_id: null,
    power_index: null,
  }

  // 价格信息
  charge_uprice_List: Charge_uprice_List[] = new Array<Charge_uprice_List>();

  // 选中价格
  charge_uprice: Charge_uprice_List = new Charge_uprice_List();

  // 用户信息
  userInfo: login_info = new login_info();

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public dialog: DialogProvider,
    public ble: BleProvider,
    public viewCtrl: ViewController,
    public user: UserInfoProvider,
    public event: EventProvider
  ) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad TimeSelectPage');
    this.charge_uprice_List.splice(0);
    this.charge_uprice_List = this.deepCopy(this.navParams.get('charge_uprice_List'));
    this.userInfo = this.user.getuserInfo();
    // for (let i = 2; i < 10; i++) {
    //   let a = {
    //     id: null,
    //     charge_time: parseFloat(this.charge_uprice_List[0].charge_time) * i, // 时长
    //     unit_price: parseFloat(this.charge_uprice_List[0].unit_price) * i// 价格
    //   }
    //   this.charge_uprice_List.push(a);
    // }
    this.power = this.navParams.get('power');
    for (let i = 0; i < this.charge_uprice_List.length; i++) {
      this.charge_uprice_List[i].id = i + 1;
    }
    this.charge_uprice = this.charge_uprice_List[0];

  }

  async ionViewDidEnter() {
    // this.ble.device_state = 'on';
    // 对不同的蓝牙连接情况采取不同的弹窗msg
    this.event.bindEvents(this.event.EVENT_BLEconnect, (res) => {
      console.log(res)
      switch (res['code']) {
        case 1: {
          this.dialog.showAlert('已经开始续充', '');
          // this.viewCtrl.dismiss({ code: 1 });
          this.navCtrl.popToRoot();
          break;
        }
        case 2: {
          this.dialog.showAlert('蓝牙连接失败请重新操作', '');
          // this.viewCtrl.dismiss({ code: 0 });
          this.ble.disconect();
          break;
        }
        case 3: {
          this.dialog.showToast('middle', '蓝牙连接成功，请选择充电时间');
          break;
        }
        case 4: {
          this.dialog.showAlert('蓝牙连接失败请重新操作', '');
          // this.viewCtrl.dismiss({ code: 0 });
          this.ble.disconect();
          break;
        }
      }
      this.event.unbindEvents(this.event.EVENT_BLEconnect, {});

    });

    let res = await this.ble.connect(this.userInfo.member_id);
    // switch(res) {
    //   case 1:{
    //     this.dialog.showAlert('已经开始续充', '');
    //     this.viewCtrl.dismiss({code:1});
    //     // this.navCtrl.popToRoot();
    //     break;
    //   }
    //   case 2:{
    //     this.dialog.showAlert('蓝牙连接失败请重新操作', '');
    //     this.viewCtrl.dismiss({code:0});
    //     break;
    //   }
    //   case 3:{
    //     this.dialog.showToast('middle', '蓝牙连接成功，请选择充电时间');
    //     break;
    //   }
    //   case 4:{
    //     this.dialog.showAlert('蓝牙连接失败请重新操作', '');
    //     this.viewCtrl.dismiss({code:0});
    //     break;
    //   }
    // }
    // .then((res) => {
    //   if (this.ble.device_state === 'off') {
    //     this.navCtrl.pop();
    //   }
    // });

  }

  ionViewDidLeave() {
    let a = this.event.unbindEvents(this.event.EVENT_BLEconnect, {})

    // if (this.ble.device_state === 'on') {
    //   this.ble.disconect();
    //   this.ble.device_state = 'off';
    // }

  }

  skipToPay() {
    if (this.ble.device_state === 'off') {
      this.dialog.showAlert('蓝牙连接失败请重新操作', '');
      this.navCtrl.pop();
      return;
    }
    if (this.charge_uprice.id) {
      // this.viewCtrl.dismiss({ power_unit_id: this.power.power_unit_id, charge_uprice: this.charge_uprice, code: 2 })
      this.navCtrl.push('ToPayPage', { power_unit_id: this.power.power_unit_id, charge_uprice: this.charge_uprice });
    } else {
      this.dialog.showAlert('没有选择时长', '');
    }
  }
  selected(data) {
    if (this.ble.device_state === 'off') {
      this.dialog.showAlert('蓝牙连接失败请重新操作', '');
      this.navCtrl.pop();
      return;
    }
    this.charge_uprice = data;
  }

  goBack() {
    this.navCtrl.pop();
    // this.viewCtrl.dismiss({ code: 0 })
  }


  deepCopy(obj) {
    let copy;

    // Handle the 3 simple types, and null or undefined
    if (null == obj || 'object' !== typeof obj) { return obj; }

    // Handle Date
    if (obj instanceof Date) {
      copy = new Date();
      copy.setTime(obj.getTime());
      return copy;
    }

    // Handle Array
    if (obj instanceof Array) {
      copy = [];
      for (let i = 0, len = obj.length; i < len; i++) {
        copy[i] = this.deepCopy(obj[i]);
      }
      return copy;
    }

    // Handle Object
    if (obj instanceof Object) {
      copy = {};
      for (const attr in obj) {
        if (obj.hasOwnProperty(attr)) {
          copy[attr] = this.deepCopy(obj[attr]);
        }
      }
      return copy;
    }

    throw new Error('Unable to copy obj! Its type isnt supported.');
  }


}





export class Charge_uprice_List {
  id: any; // 排序
  charge_time: any; // 时长
  unit_price: any; // 价格
}
export class login_info {
  member_id: any;
  nickname: any;
  phone: any;
  balance: any;
  local_balance: any;
  member_state: any;
  created_at: any;
  updated_at: any;
  cuser_id: any;
  uuser_id: any;
  is_del: any;
  password: any;
  file_type_id: any;
  access_token: any;
}
